from app.core import controller

from app.core import mysql

controllerClass = getattr(controller, "Controller")


class User_chat_group(controllerClass):
    """
    用户群聊
    """

    def __init__(self, config={}):
        """
        构造函数
        @param {Object} config 配置参数
        """
        config_init = {
            "tpl": "./user_chat_group/",
            "service": "user_chat_group",
            "get_api": ["get_group_list", "get_group_user_list", "del_group", "get_read_list"],
            "post_api": ["add_group"]
        }
        config_temp = config
        config_temp.update(config_init)
        super(User_chat_group, self).__init__(config_temp)

    def Get_group_list(self, ctx):
        """
        查询群聊
        @param {Object} ctx http请求上下文
        """
        query = dict(ctx.query)
        user_id = query.get("user_id")
        if user_id:
            sql = """
                SELECT
                    user_chat_group_id,
                    group_id,
                    group_name,
                    user_id,
                    create_time,
                    update_time
                FROM
                    user_chat_group
                WHERE
                    user_id = %s
            """ % (user_id)

            list = mysql.Service.run(self, sql=sql)

            if list and len(list) > 0:
                return {"result": {"list": list}}
            else:
                return {"result": {"list": []}}
        else:
            return {"result": {"list": []}}

    def Get_group_user_list(self, ctx):
        """
        查询群聊用户
        @param {Object} ctx http请求上下文
        """
        query = dict(ctx.query)
        group_id = query.get("group_id")

        if group_id:
            sql = """
                SELECT *
                FROM
                    `user`
                WHERE
                    user_id IN (
                        SELECT
                            user_id
                        FROM
                            user_chat_group
                        WHERE
                            group_id = %s
                    )
            """ % (group_id)

            list = mysql.Service.run(self, sql=sql)

            if list and len(list) > 0:
                return {"result": {"list": list}}
            else:
                return {"result": {"list": []}}
        else:
            return {"result": {"list": []}}

    def Add_group(self, ctx):
        """
        新增群聊
        @param {Object} ctx http请求上下文
        """
        body = dict(ctx.body)

        sql = "SELECT count(1) as count FROM user_chat_group WHERE group_id = %s and user_id = %s" % (
        body["group_id"], body["user_id"])

        count = mysql.Service.run(self, sql=sql)

        if count[0]['count'] <= 0:
            self.service.Add(body, self.config)

        if self.service.error:
            return {"error": self.service.error}
        return {"result": 1}

    def Del_group(self, ctx):
        """
        删除群聊
        @param {Object} ctx http请求上下文
        """
        query = dict(ctx.query)
        group_id = query.get("group_id")
        user_id = query.get("user_id")

        if group_id and user_id:
            result = self.service.Del({"group_id": group_id, "user_id": user_id}, self.config)

            if self.service.error:
                return {"error": self.service.error}
            result = max(result, 0)
            return {"result": result}
        else:
            return {"result": 1}

    def Get_read_list(self, ctx):
        """
        查询用户群聊未读
        @param {Object} ctx http请求上下文
        """
        query = dict(ctx.query)
        user_id = query.get("user_id")
        group_id = query.get("group_id")

        if user_id and group_id:
            sql = """
                SELECT
                    user_chat_read_id,
                    user_id,
                    send_user_id,
                    group_id,
                    `type`,
                    create_time,
                    message
                FROM user_chat_read
                WHERE
                    user_id = %s
                    and group_id = %s
                    and `type` = 2
                ORDER BY create_time asc
            """ % (user_id, group_id)

            list = mysql.Service.run(self, sql=sql)

            if list and len(list) > 0:
                return {"result": {"list": list}}
            else:
                return {"result": {"list": []}}
        else:
            return {"result": {"list": []}}